package com.tao.listener;


import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;

import javax.servlet.ServletContextEvent;

import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;

import com.tao.context.ApplicationContextUtil;
import com.tao.service.impl.ArticleServiceImpl;


public class TaokeContextListener extends ContextLoaderListener {
	
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		super.contextInitialized(sce);
		WebApplicationContext context = (WebApplicationContext)sce.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		ApplicationContextUtil.setWebApplicationContext(context);
		
		Properties resource = new Properties();
        try {
        	resource.load(TaokeContextListener.class.getClassLoader().getResourceAsStream("config.properties"));
        	for (Iterator iterator = resource.keySet().iterator(); iterator.hasNext();) {
				String key = (String) iterator.next();
				sce.getServletContext().setAttribute(key, resource.get(key));
			}
        } catch (IOException ex) {
        }
		ApplicationContextUtil.setApplication(sce.getServletContext());
		
		ArticleServiceImpl articleService = ApplicationContextUtil.getWebApplicationContext().getBean(ArticleServiceImpl.class);
		articleService.getCategoryList();
	}

}
